<?php

/**
 * Created by Sven.
 * Date: 2016/12/23
 * Time: 15:48
 *  操作cms_manage表的数据库实体类
 */
class ManageModel extends Model {
    protected  $_table = 'cms_manage'; // 该模型对应的表名

    private $id; // 管理员编号
    private $admin_user; // 管理员用户名
    private $admin_password; // 管理员密码
    private $admin_level; // 管理员等级 1 2 3

    private $last_ip;
    private $limit;

    // ManageAction需要使用本对象的私有成员属性，必须创建拦截器
    public function __get( $name ) {
        return $this->$name;
    }

    public function __set( $name, $value ) {
        // 开启输入验证
        $this->$name = Tool::mysqlString($value);
    }

    public function addManage() {

        $_sql = "INSERT 
                                  INTO 
                                  cms_manage(
                                    admin_user,
                                    admin_pass,
                                    level,
                                    reg_time
                                  ) 
                                  VALUES (
                                      '$this->admin_user',
                                      '$this->admin_password',
                                      '$this->admin_level',
                                      now()
                                  );";

        return parent::execute( $_sql );
    }

    public function deleteManage() {
        $_sql = "DELETE 
                                  FROM cms_manage
                                  
                                  WHERE  id = '$this->id'
                                  LIMIT 1;
                        ";
        return parent::execute( $_sql );
    }

    public function updateManage() {
        $_sql = "UPDATE 
                                    cms_manage 
                                    set 
                                        admin_pass='$this->admin_password', 
                                        level='$this->admin_level'
                                         
                                    where
                                      id='$this->id' 
                                    
                                    LIMIT 1; ";


        return parent::execute( $_sql );
    }

    public function getManage() {
//        $_sql = 'SELECT * FROM cms_manage'; // 单表查询
        $_sql = "SELECT 
                            m.id,
                            m.admin_user,
                            l.level_name,
                            m.login_count,
                            m.last_ip,
                            m.last_time,
                            m.reg_time
                            
                            FROM cms_manage m
                            
                            LEFT JOIN cms_level l
                                ON m.level = l.id
                             
                            ORDER BY 
                                m.id DESC
                                $this->limit; ";

        return parent::query( $_sql );
    }

    public function getSingleManageById() {
        $_sql = "SELECT 
                                id, 
                                admin_user, 
                                admin_pass,
                                level 
                            FROM 
                              cms_manage
                            WHERE
                              id = '$this->id'
                              LIMIT 1;
                             ";

        return parent::one( $_sql );
    }

    public function getManageCountByAdminUser() {
        return parent::getCount( $this->_table,'admin_user', $this->admin_user  );
    }

    public function getManageCountByLevel(){
        return parent::getCount( $this->_table,'level', $this->admin_level  );
    }


    public function getTotalCount(){
        return parent::getTotalCount($this->_table);
    }

    public function getLoginManage(){
        $_sql = "SELECT 
                                m.id, 
                                m.admin_user, 
                                l.level_name
                            FROM 
                              cms_manage m
                            LEFT JOIN cms_level l
                                ON m.level = l.id
                            WHERE
                              m.admin_user = '$this->admin_user'
                              AND 
                              m.admin_pass = '$this->admin_password'
                              LIMIT 1;
                             ";

        return parent::one( $_sql );
    }

    public function  setManageLogin(){
        $_sql = "UPDATE 
                            cms_manage
                            set 
                                    login_count = login_count+1,
                                    last_ip = '{$this->last_ip}',
                                    last_time = NOW()
                            where 
                                    id = {$this->id}
                            LIMIT 1;";

        return parent::execute($_sql);
    }


}